// hyb_yl/huiyuan/huiyuan.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    booryh: false,
    show: true,
    zfmoney: 0,
    huiyuanindex: null,
    huiyuan: []
  },
  isshow() {
    this.setData({
      show: false
    })
  },
  hide() {
    this.setData({
      show: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var bgc = wx.getStorageSync('color');
    var zid = options.zid
    var state = options.state
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var userInfo = wx.getStorageSync('userInfo')
    //查询会员是否开通
    var openid = wx.getStorageSync('openid')
    that.setData({
      zid: zid,
      state: state
    })
    this.getMymembsite()
  },
  xuanzeyh(e) {
    var t = this,
      i = e.currentTarget.dataset.dex,
      s = t.data.huiyuan;
    t.setData({
      huiyuanindex: i,
      zfmoney: s[i].aduserzhekou,
      admin_id: s[i].admin_id,
      adusername: s[i].adusername
    })
  },

  getMymembsite() {
    var that = this
    var zid = that.data.zid
    var openid = wx.getStorageSync('openid')
    app.util.request({
      'url': 'entry/wxapp/Svdocmember',
      data: {
        zid: zid,
        openid: openid,
        op: 'select'
      },
      'cachetime': '30',
      success: function(res) {
        console.log(res)
        that.setData({
          id: res.data.id,
          msg: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  kaitong(e) {
    console.log(e)
    var that = this
    var zid = that.data.zid
    var nowprice = e.detail.value.nowprice
    var rebate = e.detail.value.rebate
    var num = e.detail.value.num
    var openid = wx.getStorageSync('openid')

    app.util.request({
      'url': 'entry/wxapp/Pay',
      header: {
        "Content-Type": "application/xml"
      },
      method: "GET",
      data: {
        openid: openid,
        z_tw_money: nowprice
      },
      success: function(res) {
        console.log(res);
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr, //字符串随机数
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function(res) {
            //支付成功后加入数据库
            app.util.request({
              'url': 'entry/wxapp/Kaitmember',
              data: {
                zid: zid,
                openid: openid,
                nowprice: nowprice,
                num: num,
                rebate: rebate,
                op: 'open',
                state: 1
              },
              success: function (res) {
                console.log(res)
                wx.showToast({
                  title: '购买成功',
                  icon: 'success',
                  success: function () {
                    setTimeout(function () {
                      wx.navigateBack({
                        detail: 1
                      })
                    }, 2000)
                  }
                })
              }
            })

          }
        })
      }
    })

  }
})